<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Kurosaki <kurosaki@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

include_once(dirname(__FILE__)."/classes/ZratingObject.php");
include_once(dirname(__FILE__)."/ZadminStatisticRating.php");
include_once(dirname(__FILE__).'/ZadminGeneral.php');
include_once(dirname(__FILE__).'/ZadminStaff.php');

require_once(_PS_MODULE_DIR_.'zhelpdesk/lib/zprofess/ZhelpdeskadminTab.php');
class ZadminStatistic extends ZhelpdeskadminTab
{
	/** @var object ZadminStatisticRating() instance */
	private $adminStatisticRating;
	private $adminRatingListEmail;
	
	/** @var object ZadminGeneral() instance */
	private $general;
	/** @var object ZadminStaff() instance */
	private $staff;
	
	/**
	 * Construct 
	 */
	public function __construct()
	{
		$this->table = array('zhelpdesk_ticket_rating','id_zhelpdesk_ticket_content');
		$this->adminStatisticRating = new ZadminStatisticRating();
		$this->adminRatingListEmail = new ZadminRatingListEmail ();
		$this->general = new ZadminGeneral();
		$this->staff = new ZadminStaff();
		parent::__construct();
	}
	/**
	 * @see parent
	 * @param blolean $disable
	 */
	public function viewAccess($disable = false)
	{
		$result = parent::viewAccess($disable);
		$this->adminStatisticRating->tabAccess = $this->tabAccess;
		$this->adminRatingListEmail->tabAccess = $this->tabAccess;
		$this->general->tabAccess = $this->tabAccess; 
		$this->staff->tabAccess = $this->tabAccess;
		return $result;
	}
	/**
	 * @see parent
	 */
	public function displayErrors()
	{
		parent::displayErrors();
		$this->adminStatisticRating->displayErrors();
		$this->adminRatingListEmail->displayErrors();
		$this->general->displayErrors(); 
		$this->staff->displayErrors();
	}
	/**
	 * @see parent
	 */
	public function display()
	{
		global $currentIndex;
		
		if (Tools::isSubmit('viewzhelpdesk_ticket_rating') || Tools::getValue('id_zhelpdesk_ticket_content') || Tools::getValue('zhelpdesk_ticket_contentOrderby')) {
			if (Tools::getValue('id_zhelpdesk_ticket_rating')) {
				$_GET['id_category'] = Tools::getValue('id_zhelpdesk_ticket_rating');
				$_POST['id_category'] = Tools::getValue('id_zhelpdesk_ticket_rating');
			}
			echo '<h2>'.$this->l('List customer had rating').'</h2>';
			$this->adminRatingListEmail->display ();
			echo '<br /><br /><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to list').'</a><br />';
		} else
		 {
			$catalog_tabs = array('zhelpdesk_ticket_rating');
			// Cleaning links
			$catBarIndex = $currentIndex;
			foreach ($catalog_tabs AS $tab)
				if (Tools::getValue($tab.'Orderby') && Tools::getValue($tab.'Orderway')) 
					$catBarIndex = preg_replace('/&'.$tab.'Orderby=([a-z _]*)&'.$tab.'Orderway=([a-z]*)/i', '', $currentIndex);
			
			echo '<h2>'.$this->l('Rating gid').'</h2>';
			$this->adminStatisticRating->display($this->token);
			echo '<div style="margin:10px">&nbsp;</div>';
			$this->general->display($this->token);
			echo '<div style="margin:10px;">&nbsp;</div>';
			$this->staff->display($this->token);
			echo '<div style="margin:10px;">&nbsp;</div>';
	
		}
	}
	/**
	 * @see parent
	 */
	public function postProcess()
	{
		global $cookie;
		$this->adminStatisticRating->token=$this->token;
		$this->adminRatingListEmail->token=$this->token;
		if (Tools::getValue('id_zhelpdesk_ticket_content') || (Tools::getValue('submitFilter') )){
			$this->adminRatingListEmail->postProcess($this->token);
		}
		if (Tools::isSubmit('submitFilterzhelpdesk_ticket_rating') || Tools::isSubmit('zhelpdesk_ticket_ratingOrderway') || Tools::isSubmit('submitFilter')){
			$this->adminStatisticRating->postProcess($this->token);
		}
		
	}
}

